home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 13 / Example 13.1 / sound.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-21  |  3.1 KB  |  127 lines

  1. #include "sound.h"
  2.  
  3. //////////////////////////////////////////////////////////////
  4. //                    SOUNDFILE                                //
  5. //////////////////////////////////////////////////////////////
  6.  
  7. SOUNDFILE::SOUNDFILE()
  8. {
  9.     m_pSegment = NULL;
  10. }
  11.  
  12. SOUNDFILE::~SOUNDFILE()
  13. {
  14.     if(m_pSegment)
  15.         m_pSegment->Release();
  16.     m_pSegment = NULL;
  17. }
  18.  
  19. void SOUNDFILE::Load(WCHAR fileName[], SOUND &sound)
  20. {
  21.     //Create new segment 
  22.     CoCreateInstance(CLSID_DirectMusicSegment, NULL,
  23.                      CLSCTX_INPROC, IID_IDirectMusicSegment8,
  24.                      (void**)&m_pSegment);
  25.  
  26.     //Load from file using the loader
  27.     sound.m_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, 
  28.                                      IID_IDirectMusicSegment8, 
  29.                                      fileName, (void**)&m_pSegment);
  30.  
  31.     //Download sound to the performance interface
  32.     m_pSegment->Download(sound.m_pPerformance);
  33. }
  34.  
  35. //////////////////////////////////////////////////////////////
  36. //                    SOUND                                    //
  37. //////////////////////////////////////////////////////////////
  38.  
  39. SOUND::SOUND()
  40. {
  41.     m_pPerformance = NULL;
  42.     m_pLoader = NULL;
  43. }
  44.  
  45. SOUND::~SOUND()
  46. {
  47.     //Delete sound files
  48.     for(int i=0;i<m_sounds.size();i++)
  49.         if(m_sounds[i] != NULL)
  50.             delete m_sounds[i];
  51.     m_sounds.clear();
  52.  
  53.     //Release the loader and the performance
  54.     if(m_pLoader)
  55.         m_pLoader->Release();
  56.     m_pLoader = NULL;
  57.  
  58.     if(m_pPerformance)
  59.     {
  60.         m_pPerformance->CloseDown();
  61.         m_pPerformance->Release();
  62.     }
  63.     m_pPerformance = NULL;
  64. }
  65.  
  66. void SOUND::Init(HWND windowHandle)
  67. {
  68.     CoInitialize(NULL);
  69.  
  70.     //Create performance object
  71.     CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
  72.                      IID_IDirectMusicPerformance8, (void**)&m_pPerformance);
  73.  
  74.     //Create loader
  75.     CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  76.                      IID_IDirectMusicLoader8, (void**)&m_pLoader);
  77.  
  78.     //Initialize the performance object
  79.     m_pPerformance->InitAudio(NULL, NULL, windowHandle, 
  80.                            DMUS_APATH_SHARED_STEREOPLUSREVERB, 
  81.                            64, DMUS_AUDIOF_ALL, NULL);
  82.  
  83.     //Load sound files
  84.     std::vector<WCHAR*> fileNames;
  85.  
  86.     fileNames.push_back(L"sounds/sound1.wav");
  87.     fileNames.push_back(L"sounds/sound2.wav");
  88.     fileNames.push_back(L"sounds/sound3.wav");
  89.  
  90.     for(int i=0;i<fileNames.size();i++)
  91.     {
  92.         SOUNDFILE *snd = new SOUNDFILE();
  93.         snd->Load(fileNames[i], *this);
  94.         m_sounds.push_back(snd);
  95.     }
  96.  
  97.     SetMasterVolume(1.0f);    
  98. }    
  99.  
  100. void SOUND::PlaySound(int soundID, bool loop)
  101. {
  102.     //Faulty Sound ID
  103.     if(soundID < 0 || soundID >= m_sounds.size())return;
  104.  
  105.     //Loop sound or not
  106.     if(loop)
  107.         m_sounds[soundID]->m_pSegment->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
  108.     else m_sounds[soundID]->m_pSegment->SetRepeats(0);
  109.  
  110.     //Play Sound
  111.     m_pPerformance->PlaySegment(m_sounds[soundID]->m_pSegment, DMUS_SEGF_SECONDARY, 0, NULL);
  112. }
  113.  
  114. void SOUND::SetMasterVolume(float volume)
  115. {
  116.     //Cap volume to the range [0.0, 1.0]
  117.     if(volume < 0.0f)volume = 0.0f;
  118.     if(volume > 1.0f)volume = 1.0f;
  119.     m_masterVolume = volume;
  120.  
  121.     //Translate to the decibel range
  122.     long vol = 1000 - 5000 * (1.0f - sqrt(volume));
  123.  
  124.     //Set master volume
  125.     if(m_pPerformance)
  126.         m_pPerformance->SetGlobalParam(GUID_PerfMasterVolume, (void*)&vol, sizeof(long)); 
  127. }